package com.example.dream.learnandroid.geoquiz;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import com.example.dream.learnandroid.R;

public class CheatActivity extends AppCompatActivity {

    private static final String EXTRA_ANSWER_IS_TRUE =
            "com.example.dream.learnandroid.geoquiz.answer_is_true";
    private static final String EXTRA_ANSWER_SHOWN =
            "com.example.dream.learnandroid.geoquiz.answer_shown";

    private boolean mAnswerIsTure;
    private boolean mAnswerIsShown;

    private TextView mTextViewAnswer;
    private TextView mTextViewApiLevel;
    private Button   mBtnShowAnswer;

    public static Intent newIntent(Context packageContext, boolean answerIsTrue) {
        Intent intent = new Intent(packageContext, CheatActivity.class);
        intent.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue);
        return intent;
    }

    public static boolean wasAnswerShown(Intent result){
        return result.getBooleanExtra(EXTRA_ANSWER_SHOWN, false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);
        if(savedInstanceState != null){
            mAnswerIsShown = savedInstanceState.getBoolean(EXTRA_ANSWER_SHOWN);
        }
        mAnswerIsTure = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

        mTextViewAnswer = findViewById(R.id.anwser_text_view);
        if(mAnswerIsShown) setTextViewAnswer();

        mTextViewApiLevel = findViewById(R.id.api_levle_view);
        mTextViewApiLevel.setText(String.format(getString(R.string.api_level), Build.VERSION.SDK_INT));

        mBtnShowAnswer = findViewById(R.id.show_anwser_button);
        mBtnShowAnswer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTextViewAnswer();
            }
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(EXTRA_ANSWER_SHOWN, mAnswerIsShown);
    }

    private void setTextViewAnswer(){
        if(mAnswerIsTure){
            mTextViewAnswer.setText(R.string.true_button);
        }else{
            mTextViewAnswer.setText(R.string.false_button);
        }
        setAnswerShownResult(true);

        //动画特效
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            int cx = mBtnShowAnswer.getWidth() / 2;
            int cy = mBtnShowAnswer.getHeight() / 2;
            float radius = mBtnShowAnswer.getWidth();

            Animator anim = ViewAnimationUtils.createCircularReveal(mBtnShowAnswer, cx, cy, radius, 0);
            anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    mBtnShowAnswer.setVisibility(View.INVISIBLE);
                }
            });
            anim.start();
        }else{
            mBtnShowAnswer.setVisibility(View.INVISIBLE);
        }

    }
    private void setAnswerShownResult(boolean isAnswerShown){
        mAnswerIsShown = isAnswerShown;
        //设置返回结果
        // 以下两种方法
        // public final void setResult(int resultCode)
        // public final void setResult(int resultCode, Intent data)
        //
        // resultCode 可以是以下常量
        // Activity.RESULT_OK
        // Activity.RESULT_CANCELED
        // Activity.RESULT_FIRST_USER
        // 没有调用setResult 用户按了后退按钮，父Activity会收到 Activity.RESULT_CANCELED
        Intent data = new Intent();
        data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
        setResult(RESULT_OK, data);
    }

}
